package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.GiftCard;
import com.addplus.townmall.server.api.model.town.expand.GiftCardExpand;
import com.addplus.townmall.server.api.model.town.expand.SolrModelExpand;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 商城礼品卡 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface GiftCardMapper extends BaseAddMapper<GiftCard> {
    @Select("SELECT c.id,c.gift_card_type_id,c.gift_card_name,c.gift_card_seal_img,c.gift_value,c.online_status,c.sale_price,c.sale_status,t.name as gift_card_type_name\n" +
            "from gift_card c,gift_card_type t where c.gift_card_type_id=t.id AND t.name=#{name} AND c.is_delete=0 order by c.sort desc")
    List<GiftCardExpand> getGiftCardByTypeName(IPage page, @Param("name") String name);

    @Select("SELECT  c.*,t.name as gift_card_type_name\n" +
            "from gift_card c,gift_card_type t where c.gift_card_type_id=t.id AND c.id=#{id} AND c.is_delete=0")
    GiftCardExpand getGiftCardById(@Param("id") Integer id);

    @Select("SELECT IFNULL(max(sort)+1,1) as sort FROM gift_card  where  is_delete=0")
    Integer selectMaxSort();

    @Select("select gc.*,gcca.total_comment as gift_card_comment_num,s.store_name " +
            "from gift_card  gc " +
            "inner JOIN gift_card_comment_all gcca ON gc.id = gcca.gift_card_id " +
            "inner JOIN store  s ON s.id = gc.store_id " +
            "where ${ew.sqlSegment}")
    List<GiftCardExpand> getGiftCardExpandByQry(Page page, @Param("ew") Wrapper wrapper);

    @Select("select gc.*,gcca.total_comment as gift_card_comment_num,s.store_name " +
            "from gift_card  gc " +
            "inner JOIN gift_card_comment_all gcca ON gc.id = gcca.gift_card_id " +
            "inner JOIN gift_card_type gct ON gc.gift_card_type_id = gct.id " +
            "inner JOIN store  s ON s.id = gc.store_id " +
            "where ${ew.sqlSegment}")
    List<GiftCardExpand> getGiftCardExpandByTypeAndQry(Page page, @Param("ew") Wrapper wrapper);

    @Select("SELECT\n" +
            "\tp.id,\n" +
            "\tp.gift_card_name,\n" +
            "\tp.gift_describe,\n" +
            "\tp.gift_card_seal_img,\n" +
            "\tp.gift_card_seal_img_pc,\n" +
            "\tp.store_id,\n" +
            "\tp.sale_price,\n" +
            "\tp.line_price,\n" +
            "\tp.online_status,\n" +
            "\tp.collect_virtual_count,\n" +
            "\tp.collect_count,\n" +
            "\tp.visit_count,\n" +
            "\tp.vistit_virtual_count,\n" +
            "\tp.sale_count,\n" +
            "\tp.sale_virtual_count,\n" +
            "\tp.is_delete,\n" +
            "\tp.gmt_create,\n" +
            "\ta.total_comment,\n" +
            "\ta.good_comment,\n" +
            "\ta.middle_comment,\n" +
            "\ta.bad_comment,\n" +
            "\ta.good_rate \n" +
            "FROM\n" +
            "\tgift_card p\n" +
            "\tLEFT JOIN gift_card_comment_all a ON a.gift_card_id = p.id \n" +
            "\tAND a.is_deleted = 0 \n" +
            "WHERE\n" +
            "\tp.id = #{id} \n" +
            "\tAND p.is_delete = 0")
    SolrModelExpand getSolrModel(@Param("id") Integer id);
}
